//
//  BaseViewController.swift
//  WeiBoSwift
//
//  Created by 吴斌 on 2022/9/21.
//

import UIKit

class BaseViewController: UITableViewController {

    //懒加载属性
    lazy var visitorView : VisitorView = VisitorView.visitorView()
    
    //定义变量
    var isLogin : Bool = UserAccountViewModel.shareIntance.isLogin
    
    //系统回掉函数
    override func loadView() {
        isLogin ? super.loadView() : setupVisitorView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupNavigationItems()
      
    }
}

// MARK: - 设置UI界面
extension BaseViewController {
    // 设置访客视图
    private func setupVisitorView() {
        view = visitorView
        
        // 监听访问试图中注册和登录按钮
        visitorView.rigisterBtn.addTarget(self, action: #selector(registerBtnClick), for: .touchUpInside)
        visitorView.loginBtn.addTarget(self, action: #selector(loginBtnClick), for: .touchUpInside)
    }
    
    // 设置导航栏左右的Item
    private func setupNavigationItems() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "注册", style: .plain, target: self, action: #selector(registerBtnClick))
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "登录", style: .plain, target: self, action: #selector(loginBtnClick))
    }
}

// MARK: - 事件监听
extension BaseViewController {
    @objc private func registerBtnClick() {
        print("点击注册")
    }

    @objc private func loginBtnClick() {
        // 1.创建授权控制器
        let oauthVc = OAuthViewController()
        
        // 2.包装导航栏控制器
        let oauchNav = UINavigationController(rootViewController: oauthVc)
        oauchNav.modalPresentationStyle = .custom
        
        // 3.弹出控制器
        present(oauchNav, animated: true, completion: nil)
        
    }
}


